package com.bdh.javaBasicStudy;

import org.junit.Test;

import java.sql.SQLOutput;
import java.util.ArrayList;

/**
 * @projectName: maven_project
 * @package: com.bdh.javaBasicStudy
 * @className: javaGrammarTest
 * @author: Dream
 * @description:
 * 1、掌握Java的基础数据类型及操作
 * 2、了解public、private、protected的区别
 * 3、数组操作
 * 4、堂握条件判断if
 * 5、学握for循环。foreach循环、while循环
 * @date: 2022/6/15 09:49
 */
public class javaGrammarTest {
    /**
     * @param : void
     * @return void
     * @author dreamyouth
     * @description Java的基础数据类型及操作
     * @date 2022/6/15 09:57
     */
    @Test
    public void dataType(){
        int a = 1, b = 2;
        int c = a +b ;
        char d = 'd';
        String e = "e";
        boolean flag = true;
        float f1 = 3.14f;
        double d1 = 3.1415926d;
        f1 = c / b;
        int plus = c % b;
    }
    /**
     * @param :   void
     * @return void
     * @author dreamyouth
     * @description 了解public、private、protected的区别
     * @date 2022/6/15 10:02
     */
    @Test
    public void decorateTest(){
        User user = new User();
        Person person = new Person();
        System.out.println("public类型的数据可以直接获得     " + user.pubInfo);
        System.out.println("user可以通过调用类内的方法获得父类的protected类型的数据   " + user.getProInfo());
        System.out.println("private类型数据只能由父类调用方法获得    " + person.getPriInfo());
    }
    public class User extends Person{
        protected int account;
        protected int password;
        public String pubInfo = "user's pubInfo";

        public User() {
        }

        public User(int account, int password) {
            this.account = account;
            this.password = password;
        }

        public User(String name, String proInfo, String priInfo, int account, int password) {
            super(name, proInfo, priInfo);
            this.account = account;
            this.password = password;
        }

        public String getProInfo(){
            return super.proInfo;
        }

        public int getAccount() {
            return account;
        }

        public void setAccount(int account) {
            this.account = account;
        }

        public int getPassword() {
            return password;
        }

        public void setPassword(int password) {
            this.password = password;
        }
    }
    public class Person{
        public String name;
        protected String proInfo = "peron's proInfo";
        private String priInfo = "person's priInfo";

        public Person() {
        }

        public Person(String name, String proInfo, String priInfo) {
            this.name = name;
            this.proInfo = proInfo;
            this.priInfo = priInfo;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getProInfo() {
            return proInfo;
        }

        public void setProInfo(String proInfo) {
            this.proInfo = proInfo;
        }

        public String getPriInfo() {
            return priInfo;
        }

        public void setPriInfo(String priInfo) {
            this.priInfo = priInfo;
        }
    }
    /**
     * @param :
     * @return void
     * @author dreamyouth
     * @description 实现两个leetcode算法题 用以体现出
     * 3、数组操作
     * 4、堂握条件判断if
     * 5、学握for循环。foreach循环、while循环
     * @date 2022/6/15 10:30
     */
    @Test
    public void arrayTest(){
        /**
         * 给定一个含有 n 个正整数的数组和一个正整数 s ，
         * 找出该数组中满足其和 ≥ s 的长度最小的 连续 子数组，
         * 并返回其长度。如果不存在符合条件的子数组，返回 0。
         */
        int[] nums = {2, 3, 1, 2, 4, 3};
        System.out.println("最短子数组" + minSubArrayLen(7, nums));
        /**
         * 给你一个正整数 n ，生成一个包含 1 到 n2 所有元素，
         * 且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix 。
         */
        int n = 3;
        int[][] res = generateMatrix(n);
        System.out.println("回文数");
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(res[i][j] + " ");
            }
            System.out.println();
        }

    }
    public int minSubArrayLen(int target, int[] nums){
        int left = 0;
        int sum = 0;
        int res = Integer.MAX_VALUE;
        for(int right = 0; right < nums.length; right++){
            sum += nums[right];
            while(sum >= target){
                res = Math.min(res, right - left + 1);
                sum -= nums[left++];
            }
        }
        return res == Integer.MAX_VALUE ? 0 : res;
    }
    public int[][] generateMatrix(int n) {
        int loop = 0;
        int[][] res = new int[n][n];
        int start = 0;
        int count = 1;
        int i, j;//j为col， i为row

        while(loop++ < n/2){
            //右->左
            for (j = start; j < n - loop; j++){
                res[start][j] = count++;
            }

            //上->下
            for (i = start; i < n - loop; i++){
                res[i][j] = count++;
            }

            //右->左
            for (; j > loop; j--){
                res[i][j] = count++;
            }

            //下->上
            for (; i > loop; i--){
                res[i][j] = count++;
            }

            start++;
        }
        if(n % 2 == 1){
            res[start][start] = count;
        }
        return res;
    }

}
